<?php
namespace src;
use classlib\acl;
use classlib\input;
use src\logic\Cadmin;
use classlib\core\lang;
use classlib\core as C;
use classlib\core\controller;
class Cmyself extends controller {

	public function main() {
		$view   = C\view::inst('extjs');
		$userid = Cadmin::chkaccess($view);
		$user   = C\model::load('main.sys.user')->get_one($userid);
		if ($user['expire'] < 1) {
			$user['expire'] = 365 * 3600 * 24;
		}
		$user['expire'] = date('Y-m-d', $user['expire']);
		$where  = array('userid'=>$userid);
		$roles  = C\model::load('main.sys.ruser')->get_column(0, -1, $where, 'ruleid');
		$roles  = array_unique($roles);
		$list   = C\model::load('main.sys.role')->get_list(0, -1);
		$mroles = array();
		if ($list) {
			foreach ($list as &$item) {
				if (in_array($item['id'], $roles)) {
					$mroles[] = array('inputValue'=>$item['id'], 'checked'=>true,
						'boxLabel'=>$item['name'], 'name'=>'roles[]');
				}
			}
		}
		if (!$mroles) {
			$lang   = C\lang::inst('adm');
			$drole  = $lang->get('common', '40012');
			$mroles = array(array('inputValue'=>1, 'checked'=>true,
				'boxLabel'=>$drole, 'name'=>'roles[]'));
		}
		$dlang = lang::inst('dd');
		$view->assign_by_ref('user', $user);
		$view->assign_by_ref('mroles', $mroles);
		$view->assign('isdup', $dlang->get('sh0002'));
		$view->assign_by_ref('title', Cadmin::$name);
		$view->display('core/myself.ext.js');
	}
	
	/**
	 * 获取权限交接列表
	 * Enter description here ...
	 */
	public function agentlog() {
		$view   = C\view::inst('json');
		$userid = Cadmin::islogin($view);
		$where  = array(array('agentid', 0, \classlib\orm::OP_GT), array('('), array('userid', $userid), 
			array('agentid', $userid, \classlib\orm::OP_EQ, \classlib\orm::OP_OR), array(')'));
		$list   = C\model::load('main.sys.ruser')->get_list(0, -1, $where);
		if (!empty($list)) {
			$orm  = C\model::load('main.sys.user');
			$rorm = C\model::load('main.sys.role');
			foreach ($list as &$item) {
				$user = $orm->get_one($item['userid']);
				$agent= $orm->get_one($item['agentid']);
				$item['user']  = $user['account'].'['.$user['nickname'].']';
				$item['agent'] = $agent['account'].'['.$agent['nickname'].']';
				$item['stime'] = date('Y-m-d H:i:s', $item['stime']);
				$role = $rorm->get_one($item['ruleid']);
				$item['name']  = ($role)? $role['name']:'Unknown';
			}
		}
		$view->assign_by_ref('dataset', $list);
		$view->display();
	}
	
	/**
	 * 做权限交接
	 * Enter description here ...
	 */
	public function doagent() {
		$view   = C\view::inst('json');
		$agentid= Cadmin::chkaccess($view);
		$userid = (int)input::post('userid');
		$lang   = C\lang::inst('adm');
		if ($userid < 0 || $userid == $agentid) {
			$view->assign_error('userid', $lang->get('common', '40013'));
		}
		$remark = trim(input::post('remark'));
		$roles  = input::post('roles');
		if (is_array($roles)) {
			$roles = array_filter($roles, 'is_numeric');
		}
		if(empty($roles) || !is_array($roles)) {
			$view->assign_error('roles', $lang->get('common', '40015'));
		}
		//验证
		$where = array('userid'=>$agentid, array('ruleid', $roles, \classlib\orm::OP_IN));
		$total = C\model::load('main.sys.ruser')->get_total($where);
		if ($total != count($roles)) {
			$view->assign_error('roles', $lang->get('common', '40016'));
		}
		//添加角色
		$orm   = C\model::load('main.sys.ruser')->get_orm();
		$sorm  = C\model::load('main.sys.role');
		$idsarr= array();
		foreach ($roles as &$role) {
			if (!is_numeric($role) || $role < 1) {
				continue;
			}
			$data = $sorm->get_one($role);
			$idsarr[] = $data['name'].'['.$role.']';
			$data = array('userid'=>$userid, 'stime'=>C\config::$nowtime,
				 'ruleid'=>$role, 'agentid'=>$agentid, 'remark'=>$remark);
			$orm->duplicate(array('agentid'=>$agentid, 'remark'=>$remark))->insert($data);
		}
		//删除角色
		$orm->where('userid', $agentid)->where('ruleid', $roles, \classlib\orm::OP_IN)->delete();
		$user= C\model::load('main.sys.user')->get_one($userid);
		$user= $user['account'].'['.$userid.']';
		$msg = sprintf($lang->get('common', '40017'), $user, implode(',', $idsarr)).'::=>'.$remark;
		Cadmin::log2db($msg);
		$view->assign('msg', $lang->get('common', '40018'));
		$view->display();
	}
	
	public function dosave() {
		$view   = C\view::inst('json');
		$userid = Cadmin::islogin($view);
		$user   = array();
		$filter = new \classlib\validator\auditor();
		$lang   = C\lang::inst('adm');
		$filter->lang($lang);
		try {
			$user['email']    = $filter->reset('email')->required()->email()->run();
			$user['mobile']   = $filter->reset('mobile')->required()->mobile()->run();
			if (input::get_post('loginpw')) {
				$user['loginpw'] = $filter->reset('loginpw')->nospace()->minlen(6)->run();
				$user['loginpw'] = acl::inst()->crypt($user['loginpw']);
			}
			$acl = acl::inst();
			$res = \src\logic\upfile::inst($acl)->post('avtar');
			if ($res) {
				$user['avtar'] = $res['url'];
				$view->assign('url', $user['avtar']);
			}
		} catch (\classlib\validator\AuditorException $e) {
			$view->assign_error($e->getField(), $e->getMessage());
		} catch (\Exception $e) {
			$view->assign_error($e->getCode(), $e->getMessage());
		}
		C\model::load('main.sys.user')->save($userid, $user);
		$view->assign('msg', $lang->get('common', '40019'));
		$view->display();
	}
}